// export default {
//     formatDate,
//     formatTime
// }
export let formats = {
  formatDate,
  formatTime,
};
function formatDate(time) {
  var date = new Date(time);
  var year = date.getFullYear(); //返回4位数的年份
  var month = zero(date.getMonth() + 1); //返回0-11
  var day = zero(date.getDate()); //1-31
  var hour = zero(date.getHours()); //0-23
  var minute = zero(date.getMinutes()); //0-59
  var second = zero(date.getSeconds()); //0-59
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

/**
 *
 * @param {number} number --数字时间戳
 * @param {string} format -- 自定义格式的时间显示字符串
 * @returns {string} --格式化好时间戳为自定义格式的事件字符串
 */
function formatTime(number, format) {
  let formatArr = ["Y", "M", "D", "h", "m", "s"];
  let returnArr = [];

  let date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(zero(date.getMonth() + 1));
  returnArr.push(zero(date.getDate()));
  returnArr.push(zero(date.getHours()));
  returnArr.push(zero(date.getMinutes()));
  returnArr.push(zero(date.getSeconds()));

  for (let item in returnArr) {
    format = format.replace(formatArr[item], returnArr[item]);
  }
  return format;
}

function zero(n) {
  n = n.toString();
  return n[1] ? n : `0${n}`;
}
